﻿' Note: For instructions on enabling IIS6 or IIS7 classic mode, 
' visit http://go.microsoft.com/?LinkId=9394802
Imports System.Web.Http
Imports System.Web.Optimization

Public Class WebApiApplication
    Inherits System.Web.HttpApplication

    Sub Application_Start()
        AreaRegistration.RegisterAllAreas()

        WebApiConfig.Register(GlobalConfiguration.Configuration)
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters)
        RouteConfig.RegisterRoutes(RouteTable.Routes)
        BundleConfig.RegisterBundles(BundleTable.Bundles)

        App.MapGeoDataApp.Initialize()
    End Sub

    Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "token")
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
        If (Request.Headers.AllKeys.Contains("Origin") And Request.HttpMethod = "OPTIONS") Then
            Response.Flush()
        End If

        If (Request.Headers.AllKeys.Contains("token") AndAlso Request.Headers("token") IsNot Nothing AndAlso Request.Headers("token") <> "") Then
            Services.SessionEngine.getInstance().markAsInUse(Request.Headers("token"))
            'Services.GeneralStats.Log(Request)
        End If
    End Sub
End Class